package jp.co.systemi.asp.utils;

import java.util.logging.Logger;

import jp.co.systemi.asp.workflow.ICallback;
import jp.co.systemi.asp.workflow.Main;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

public class InputBoxDialog extends WebPage{
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(InputBoxDialog.class.getName());
    /** フィードバック */
    protected FeedbackPanel feedback = new FeedbackPanel("wi_feedback");
    String inputText = "";
    public InputBoxDialog(final Main mainPage, final DModalWindow window) {
        init(mainPage, window);
    }
    public void init(final Main mainPage, final DModalWindow window) {
        feedback.setOutputMarkupId(true);
        add(feedback);

        Form<Object> form = new Form<Object>("wi_form");
        this.add(form);
        TextArea<String> wi_message = new TextArea<String>("wi_message", new PropertyModel<String>(this, "inputText"));
//        wi_message.setEscapeModelStrings(false);
        form.add(wi_message);
        form.add(new AjaxButton("wi_OK") {
            private static final long serialVersionUID = 5873629255088464587L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                PageParameters pars = new PageParameters();
                pars.add("input", "OK");
                String msg = ((ICallback)mainPage).getEmptyMessage(pars);
                if(org.datanucleus.util.StringUtils.isEmpty(inputText) && !org.datanucleus.util.StringUtils.isEmpty(msg)){
                    error(msg);
                    target.addComponent(feedback);
                    return;
                }
//                pars.add("inputText", inputText);
//                ((ICallback)mainPage).setResult(pars);
                window.setWindowClosedCallback(window.getOKActionCallback());
                window.setRetValue(inputText);
                window.close(target);
            }

        });
        form.add(new AjaxButton("wi_Cancel") {
            private static final long serialVersionUID = 5873629255088464587L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                window.setWindowClosedCallback(window.getCancelActionCallback());
                window.close(target);
            }
        }.setDefaultFormProcessing(false));
    }
}
